<template>
	<div class="page">
		<el-form class="demo-ruleForm" :model="userInfo" ref="userInfo" label-width="100px" style="text-align: left" status-icon :rules="rules">
			<el-form-item label="登录账号" prop="">
				<el-input type="text" v-model="userInfo.userCode" auto-complete="off" :disabled="true"></el-input>
			</el-form-item>
			<el-form-item label="用户名" prop="userName">
				<el-input type="text" v-model="userInfo.userName" auto-complete="off"></el-input>
			</el-form-item>
			<el-form-item label="邮箱" prop="userName">
				<el-input type="text" v-model="userInfo.emil" auto-complete="off"></el-input>
			</el-form-item>
			<el-form-item label="创建时间" prop="">
				<el-input type="text" v-model="userInfo.createDate" auto-complete="off" :disabled="true"></el-input>
			</el-form-item>
			<el-form-item label="备注信息" prop="">
				<el-input type="textarea" autosize v-model="userInfo.remark" auto-complete="off" :disabled="true"></el-input>
			</el-form-item>
			<el-form-item label="头像" prop="">
				<el-upload class="avatar-uploader" :action="actionUrl" name="file" with-credentials :show-file-list="false" :on-progress="progress" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
					<img v-if="userInfo.imageUrl" :src="userInfo.imageUrl" class="avatar">
					<i v-else class="el-icon-plus avatar-uploader-icon"></i>
				</el-upload>
			</el-form-item>
		</el-form>
		<div slot="footer" class="dialog-footer" style="text-align: center;">
			<el-button type="primary" @click="submitForm(userInfo)" size="small" :loading="newSaveButtonStatus">提交修改</el-button>
		</div>
	</div>

</template>
<style>
	@import url("../../common/common.css");
	@import url("userInfo.css");
</style>
<script>
	import $ from 'jquery'
	export default {
		data() {
			var validatePass = (rule, value, callback) => {
				if(value === '') {
					callback(new Error('不能为空'));
				} else {
					callback();
				}
			};
			return {
				userInfo: {
					userName: '',
					imageUrl: ''
				},
				newSaveButtonStatus: false,
				rules: {
					userName: [{
						required: true,
						validator: validatePass,
						trigger: 'blur'
					}]
				},
				actionUrl: this.utils.rootUrl + 'upload/uploadFile',
				user: null
			}
		},
		created() {
			this.user = this.utils.getJsonObject('user');
			this.getUserInfo();
		},
		mounted() {},
		methods: {
			getUserInfo() {
				var _self = this;
				_self.utils.ajaxJSON({
					type: 'post',
					url: 'userManager/getUserInfo',
					data: {
						userCode: _self.user.userCode
					},
					success: function(res) {
						res.data.createDate = new Date(res.data.createDate).Format('yyyy-MM-dd hh:mm:ss');
						_self.userInfo.userCode = res.data.userCode;
						_self.userInfo.userName = res.data.userName;
						_self.userInfo.emil = res.data.emil;
						_self.userInfo.createDate = res.data.createDate;
						_self.userInfo.remark = res.data.remark;
						_self.userInfo.imageUrl = _self.utils.imageRootUrl + res.data.imageUrl;
					}
				})
			},
			handleAvatarSuccess(res, file) {
				//				this.userInfo.imageUrl = res.data.httpUrl;
				this.$set(this.userInfo, 'imageUrl', this.utils.imageRootUrl + res.data.httpUrl)
				this.userInfo.fileId = res.data.fileId;
				console.log(this.userInfo)
			},
			beforeAvatarUpload(file) {
				//const isJPG = file.type === 'image/jpeg';
				const isLt50k = file.size / 1024 < 50;
//				if(!isJPG) {
//					this.$message.error('上传头像图片只能是 JPG 格式!');
//				}
				if(!isLt50k) {
					this.$message.error('上传头像图片大小不能超过 50KB!');
				}
				return isLt50k;
//				return true;
			},
			submitForm(userInfo) {
				var _self = this;
				_self.$refs['userInfo'].validate((valid) => {
					if(valid) {
						_self.utils.ajaxJSON({
							type: 'POST',
							url: 'userManager/updateUserInfo',
							data: userInfo,
							success: function(res) {
								if(res.status == 200) {
									_self.$message({
										type: 'success',
										message: '修改成功!'
									});
								}
							}
						})
					}
				});
			},
			progress(event, file, fileList) {
				console.log(JSON.stringify(file))
			}
		}
	}
</script>